export default function ValidatorBuilder (condition, msg, ...params) {

  // value 占位符
  // "#val>10"
  // "#val>10&&#val<100"

  return (rule, value, callback) => {

    let fmtCondition = condition.replace(/#val/g, value);
    //format "#val" of msg
    msg = msg.replace(/#val/g, value);
    //format other params
    msg = format(msg, params);

    if (!eval(fmtCondition)) {
      return callback(new Error(msg));
    } else {
      callback();
    }
  };

}

/**
 * "{0}你好,{1}我是你大爷,{2}今天happy吗?"
 * @param str
 * @param params
 * @returns {*}
 */
function format (str, ...params) {
  return str.replace(/\{(\d+)\}/g, function (m, n) {
    return params[n];
  });
};
